package com.jucai.pppeiyouclient.view.fragment;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.SystemNewsAdapter;
import com.jucai.pppeiyouclient.base.BaseFragment;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.bean.SystemNewsBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.view.activity.SystemMsgDeatil;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Kuky on 2017-05-02.
 */

public class SystemNewsFragment extends BaseFragment implements PullToRefreshLayout.OnRefreshListener {
    private static final String TAG = "SystemNewsFragment : ";
    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView systemNewsRv;
    private List<SystemNewsBean> systemNews = new ArrayList<>();
    private SystemNewsAdapter systemNewsAdapter;
    private Handler mHandler = new Handler();
    private int page = 1;

    @Override
    protected void initView() {
        systemNewsAdapter = new SystemNewsAdapter(getActivity(), systemNews);
        refreshLayout = (PullToRefreshLayout) itemView.findViewById(R.id.refresh_view);
        systemNewsRv = (PullToRefreshForRecyclerView) itemView.findViewById(R.id.content_view);
        systemNewsRv.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        systemNewsRv.setItemAnimator(new DefaultItemAnimator());
        systemNewsRv.setAdapter(systemNewsAdapter);
        systemNewsAdapter.setOnItemClickListener(new CommonRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                Intent i = new Intent(getContext(), SystemMsgDeatil.class);
                if(!systemNews.isEmpty()){
                    SystemNewsBean bean = systemNews.get(position);
                    i.putExtra("mail_title",bean.getMail_title());
                    i.putExtra("mail_text",bean.getMail_text());
                }
                startActivity(i);
            }

            @Override
            public void onItemLongClickListener(View view, int position) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Accompany_Mail.getMailList");
        param.put("page", "1");

        CommonRequestNetData.getJsonArrayData(getActivity(), param,true, new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                LogUtils.e(TAG + "jsArr = " + jsonArray.toString());
                systemNews = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<SystemNewsBean>>() {
                }.getType());
                LogUtils.e(TAG + "news = " + systemNews);
                systemNewsAdapter.setData(systemNews);
            }
        });
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected int getFragmentViewId() {
        return R.layout.common_fragment_with_ptr;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadNetData();
                refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        ++page;
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Accompany_Mail.getMailList");
                param.put("page", page + "");

                CommonRequestNetData.getJsonArrayData(getActivity(), param, true,new CommonRequestNetData.JsonArrayCallBack() {
                    @Override
                    public void parseJsonArray(JSONArray jsonArray) {
                        LogUtils.e(TAG + "jsArr = " + jsonArray.toString());
                        List<SystemNewsBean> list = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<SystemNewsBean>>() {
                        }.getType());
//                        systemNews.addAll(list);
                        LogUtils.e(TAG + "news = " + systemNews);
                        systemNewsAdapter.setData(systemNews);
                    }
                });
                refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }
}
